let assert = require('assert');
let request = require('supertest');
let app = "http://127.0.0.1:8080";
let nightwatch = require('nightwatch');
let config = require('../nightwatch.json');

const cv = require('opencv');

function getHistogram(mat) {
  let arr = [];
  for (var i = 0; i < 64; i++) {
      arr[i] = 0;
  }
  let pow4 = Math.pow(4, 2);
  let data = mat.toBuffer(); 
  for (var i = 0, len = data.length; i < len; i += 4) {
      var red = (data[i] / 64) | 0;
      var green = (data[i + 1] / 64) | 0;
      var blue = (data[i + 2] / 64) | 0;
      var index = red * pow4 + green * 4 + blue;
      arr[index]++;
  }

  return arr;
}

function cosine(arr1, arr2) {
  var axb = 0,
      a = 0,
      b = 0;
  for (var i = 0, len = arr1.length; i < len; i++) {
      axb += arr1[i] * arr2[i];
      a += arr1[i] * arr1[i];
      b += arr2[i] * arr2[i];
  }
  return axb / (Math.sqrt(a) * Math.sqrt(b));
} 

let client = nightwatch.initClient(config.test_settings.default);
let browser = client.api();

before(function() {
  // runs before all tests in this block
  // console.log('before');
});

after(function() {
  // runs after all tests in this block
  // console.log('after');
});

beforeEach(function() {
  // runs before each test in this block
  // console.log('beforeEach');
});

// beforeEach('对象：Array', function() {
  // runs before each test in this block
  // console.log('beforeEach Array');
// });

afterEach(function() {
  // runs after each test in this block
  // console.log('afterEach');
});

// test cases
describe('对象：Array', function() {
  this.timeout(500000);
  
  // 方法
  describe('方法：indexOf()', function() {
    it('当没有匹配上的时候需要返回-1', function() {
      assert.equal([1,2,3].indexOf(4), -1);
    });
  });

  // 数据接口
  describe('测试post',function(){
    it('post /login',function(done){
      request(app)
        .post('/login')
        .set('Content-Type','json')
        .expect('Content-Type', /json/)
        .expect(200)
        .end(function(err, res) { 
          if (err) { return done(err);}
          assert.ok(res.body.status);
          done();
        });
    });
  });


  describe('测试ui', function() {
    it('demo', function(done) {
      // 可以通过done函数直接进行异步测试 
      browser
        .maximizeWindow()
        .url('https://www.baidu.com')
        .waitForElementVisible('body', 1000)
        .setValue('#kw', '衷于栖')
        .click('#su')
        .pause(3000)
        .assert.containsText('#wrapper_wrapper', '衷于栖')
        .saveScreenshot('./screenshots/fileName.png')
        .end();

      client.start(done);
    });
  });

  describe('测试opencv', function() {
    it('测试', function(done) {
      
      let sourcePath = './screenshots/a.png';
      let tempPath = './screenshots/b.png';
      cv.readImage(sourcePath, (err, sourceImg) => {
        if (err) return console.error('error loading image');
        let result = sourceImg.matchTemplate(tempPath, 5);
        // console.log("获得：" + result);
        // console.log('top left:', result[1], result[2], ' top right: ', result[1] + result[3], result[2]);
        // console.log('bottom left:', result[1], result[2] + result[4], ' bottom right: ', result[1] + result[3], result[2] + result[4]);
        // sourceImg.rectangle([result[1], result[2]],[result[1] + result[3], result[2]],
        //   [result[1], result[2] + result[4]],[ result[1] + result[3], result[2] + result[4]]);
        // sourceImg.save('./screenshots/test.png');
        // 截取结果图 与 b 图进行对比 最终获得一个百分比相似度
        let newMat = sourceImg.crop(result[1], result[2], result[3], result[4]);
        cv.readImage(tempPath, (error, tempImg) => {
          if (error) return console.error('error loading image');

          let per = cosine(getHistogram(newMat),getHistogram(tempImg));
          newMat.save('./screenshots/test.png');
          console.log('相似度：' , per); 
          assert.ok(true);
        });

        done();
      });

      
    });
  });

});




